package com.dididoctor.doctor.Activity.Main.Message;

import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Activity.Usercentre.Approve.ApproveActivity;
import com.dididoctor.doctor.Activity.Usercentre.Authentication.AuthenticationActivity;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.EduActivity;
import com.dididoctor.doctor.MV.FastHttpResponseHandler;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.R;
import com.dididoctor.doctor.Ui.customlistview.OnRefreshListener;
import com.dididoctor.doctor.Ui.customlistview.RefreshListView;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 张强
 * Created by Administrator on 2016/4/19.
 */
public class MessageActivity extends EduActivity implements MessageView {
    private TextView mTvTitle;
    private ImageView mBtnBack;
    private RefreshListView mlvMessage;
    private MessageAdapter adapter;
    private MessagePresenter presenter;
    private int pageIndex = 1;
    private String isPass = "";

    private List<MessageBean> messageData = new ArrayList<MessageBean>();

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_message);
    }

    @Override
    public void init() {

        /**头部**/
        initTitleView();

        /** 界面**/
        initview();

    }

    private void initview() {

        mlvMessage = (RefreshListView) findViewById(R.id.lv_message);
        mlvMessage.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getOrderList(pageIndex);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getOrderList(pageIndex);
            }

            @Override
            public void onScrollListener(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
        mlvMessage.setHeadAndFoot(true, true);
        adapter = new MessageAdapter(MessageActivity.this, messageData, new MessageAdapter.toApprevoClick() {
            @Override
            public void toApprevo() {
                getisPass();
            }
        });
        mlvMessage.setAdapter(adapter);
        presenter = new MessagePresenter(MessageActivity.this, this);
        presenter.getOrderList(pageIndex);
    }

    private void initTitleView() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvTitle.setText("消息");
        mBtnBack = (ImageView) findViewById(R.id.btn_back);
    }

    @Override
    public void onClickEvent(View view) {

    }

    @Override
    public void getdoctorfail() {
        showToastMessage("网络连接失败，请检查您的网络设置");
        mlvMessage.onRefreshFinish();
    }

    @Override
    public void getdoctorsucced(List<MessageBean> messageBeans) {
        if (messageBeans != null && messageBeans.size() > 0) {
            //没有评价内容
            if (pageIndex == 1) {
                this.messageData.clear();
                adapter.setData(messageBeans);
                mlvMessage.onRefreshFinish();
            } else {
                if (messageBeans.size() < ConstantValue.LOAD_MESSAGE_COUNT) {
                    adapter.addData(messageBeans);
                    mlvMessage.showNoMoreData();
                } else {
                    adapter.addData(messageBeans);
                    mlvMessage.hideNoMoreData();
                }
            }
            adapter.notifyDataSetChanged();
        } else {
            if (pageIndex == 1) {
                mlvMessage.onRefreshFinish();
            } else {
                mlvMessage.showNoMoreData();
                adapter.notifyDataSetChanged();
            }
        }
    }

    public void getisPass() {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.userDetail, params, new FastHttpResponseHandler() {
            @Override
            public void onResponse(Response response) {
                if (response.isSucceed()) {
                    Map<String, Object> map = response.getMap("doctorInfo");
                    isPass = Util.toString(map.get("isPass"));
                    Token.setIsPass(isPass);
                    // 是否认证1-未认证2-申请认证3-认证成功4-认证失败
                    Intent intent;
                    if ("4".equals(isPass)) {
                        intent = new Intent(MessageActivity.this, AuthenticationActivity.class);
                    } else {//审核中
                        intent = new Intent(MessageActivity.this, ApproveActivity.class);
                    }
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else {
                    showToastMessage(response.getMsg());
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                showToastMessage("网络错误");
            }
        });
    }
}
